package com.authine.cloudpivot.app.openapi.controller;

import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.app.openapi.dto.GenerateTokenReq;
import com.authine.cloudpivot.app.openapi.dto.GenerateTokenRes;
import com.authine.cloudpivot.app.openapi.dto.OpenUserInfoRes;
import com.authine.cloudpivot.app.openapi.service.OpenApiServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author lifuchun
 */
@RestController
public class OpenApiController {

    private final OpenApiServiceImpl openApiServiceImpl;

    public OpenApiController(OpenApiServiceImpl openApiServiceImpl) {
        this.openApiServiceImpl = openApiServiceImpl;
    }

    @ApiOperation("生成用户令牌")
    @PostMapping("/token/generate")
    public SingleResponse<GenerateTokenRes> generateToken(@RequestBody GenerateTokenReq param) {
        return SingleResponse.of(openApiServiceImpl.generateToken(param.getAppKey(),param.getUserId(),param.getNonce(),param.getSign()));
    }

    @ApiOperation("通过令牌获取用户信息")
    @GetMapping("/user/info")
    public SingleResponse<OpenUserInfoRes> getUserInfo(@RequestHeader("token") String openToken) {
        return SingleResponse.of(openApiServiceImpl.getUserInfo(openToken));
    }


}